package com.droidnova.android.games.vortex;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
  
public class Cube {
   private FloatBuffer vertexBuffer;  // Buffer for vertex-array
   private int numFaces = 6;
   private float face = 0.5f;
   
   private float[][] colors = {  // Colors of the 6 faces
      {1.0f, 0.5f, 0.0f, 1.0f},  // 0. orange
      {1.0f, 0.0f, 1.0f, 1.0f},  // 1. violet
      {0.0f, 1.0f, 0.0f, 1.0f},  // 2. green
      {0.0f, 0.0f, 1.0f, 1.0f},  // 3. blue
      {1.0f, 0.0f, 0.0f, 1.0f},  // 4. red
      {1.0f, 1.0f, 0.0f, 1.0f}   // 5. yellow
   };
  
   private float[] vertices = {  // Vertices of the 6 faces
      // FRONT
		   -face, -face,  face,  // 0. left-bottom-front
	       face, -face,  face,  // 1. right-bottom-front
	      -face,  face,  face,  // 2. left-top-front
	       face,  face,  face,  // 3. right-top-front
	      // BACK
	       face, -face, -face,  // 6. right-bottom-back
	      -face, -face, -face,  // 4. left-bottom-back
	       face,  face, -face,  // 7. right-top-back
	      -face,  face, -face,  // 5. left-top-back
	      // LEFT
	      -face, -face, -face,  // 4. left-bottom-back
	      -face, -face,  face,  // 0. left-bottom-front 
	      -face,  face, -face,  // 5. left-top-back
	      -face,  face,  face,  // 2. left-top-front
	      // RIGHT
	       face, -face,  face,  // 1. right-bottom-front
	       face, -face, -face,  // 6. right-bottom-back
	       face,  face,  face,  // 3. right-top-front
	       face,  face, -face,  // 7. right-top-back
	      // TOP
	      -face,  face,  face,  // 2. left-top-front
	       face,  face,  face,  // 3. right-top-front
	      -face,  face, -face,  // 5. left-top-back
	       face,  face, -face,  // 7. right-top-back
	      // BOTTOM
	      -face, -face, -face,  // 4. left-bottom-back
	       face, -face, -face,  // 6. right-bottom-back
	      -face, -face,  face,  // 0. left-bottom-front
	       face, -face,  face   // 1. right-bottom-front
   };
        
   // Constructor - Set up the buffers
   public Cube() {
      // Setup vertex-array buffer. Vertices in float. An float has 4 bytes
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
      vbb.order(ByteOrder.nativeOrder()); // Use native byte order
      vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
      vertexBuffer.put(vertices);         // Copy data into buffer
      vertexBuffer.position(0);           // Rewind
   }
  
   // Draw the shape
   public void draw(GL10 gl) {
      gl.glFrontFace(GL10.GL_CCW);    // Front face in counter-clockwise orientation
      gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
      gl.glCullFace(GL10.GL_BACK);    // Cull the back face (don't display)
  
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

      // Render all the faces
      for (int face = 0; face < numFaces; face++) {
         // Set the color for each of the faces
         gl.glColor4f(colors[face][0], colors[face][1], colors[face][2], colors[face][3]);
         // Draw the primitive from the vertex-array directly
         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face*4, 4);
      }
      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glDisable(GL10.GL_CULL_FACE);
   }
}
